﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tools.Core;
using Tools.Core.Entities;
using Tools.Services.Interfaces;

namespace Tools.Services
{
    public class MailReceiverService : IMailReceiverService
    {
        private readonly DB _db;
        public MailReceiverService(DB db)
        {
            _db = db;
        }
        public async Task CreateReceiverAsync(MailReceiver receiver)
        {
            _db.MailReceivers.Add(receiver);
            await _db.SaveChangesAsync();
        }

        public async Task DeleteReceiverAsync(int id)
        {
            var receiver= await _db.MailReceivers.FirstOrDefaultAsync(r=>r.Id==id);
            if (receiver != null)
            {
                _db.MailReceivers.Remove(receiver);
                await _db.SaveChangesAsync();
            }
        }

        public async Task<IEnumerable<MailReceiver>> GetAllReceiversAsync()
        {
            return await _db.MailReceivers.ToListAsync();
        }

        public async Task<MailReceiver> GetReceiverAsync(int id)
        {
            return await _db.MailReceivers.FirstOrDefaultAsync(r => r.Id == id);
        }

        public async Task UpdateReceiverAsync(MailReceiver receiver)
        {
            _db.MailReceivers.AddOrUpdate(receiver);
            await _db.SaveChangesAsync();
        }
    }
}
